package a10_动态规划;

import java.util.Arrays;

/**
 * <p>
 * a24_完全平方数复习3
 * </p>
 *
 * @author flyduck
 * @since 2025/4/16
 */
public class a24_完全平方数复习3 {
    public int numSquares(int n) {
        int[] dp = new int[n+1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;

        for (int i = 1; i <= n; i++) {
            for (int j = i*i; j <= n; j++) {
                if(dp[j-(i*i)] != Integer.MAX_VALUE){
                    dp[j] = Math.min(dp[j],dp[j-(i*i)] + 1);
                }
            }
        }

        return dp[n] != Integer.MAX_VALUE ? dp[n] : 0;
    }
}
